package com.bank.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import javax.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 交易信息实体类
 * 对应数据库表：trade_info
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2025-06-19
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("trade_info")
public class TradeInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 交易ID-主键
     */
    @TableId(value = "trade_id", type = IdType.AUTO)
    private Integer tradeId;

    /**
     * 卡号-外键
     */
    @TableField("card_id")
    @NotBlank(message = "卡号不能为空")
    @Pattern(regexp = "^[0-9]{16}$", message = "卡号格式不正确，应为16位数字")
    private String cardId;

    /**
     * 交易日期-默认当前时间
     */
    @TableField(value = "trade_date", fill = FieldFill.INSERT)
    private LocalDateTime tradeDate;

    /**
     * 交易类型-存入/支取/转账
     */
    @TableField("trade_type")
    @NotBlank(message = "交易类型不能为空")
    @Pattern(regexp = "^(存入|支取|转账)$", message = "交易类型只能是：存入、支取、转账")
    private String tradeType;

    /**
     * 交易金额-必须大于0
     */
    @TableField("trade_money")
    @NotNull(message = "交易金额不能为空")
    @DecimalMin(value = "0.01", message = "交易金额必须大于0")
    @Digits(integer = 13, fraction = 2, message = "交易金额格式不正确")
    private BigDecimal tradeMoney;

    /**
     * 备注信息
     */
    @TableField("remark")
    @Size(max = 200, message = "备注信息长度不能超过200个字符")
    private String remark;

    /**
     * 转账目标卡号-转账时使用
     */
    @TableField("target_card_id")
    @Pattern(regexp = "^[0-9]{16}$|^$", message = "目标卡号格式不正确，应为16位数字")
    private String targetCardId;

    /**
     * 银行卡信息（关联查询时使用）
     */
    @TableField(exist = false)
    private CardInfo cardInfo;

    /**
     * 目标银行卡信息（关联查询时使用）
     */
    @TableField(exist = false)
    private CardInfo targetCardInfo;

    /**
     * 构造函数
     */
    public TradeInfo() {
    }

    /**
     * 带参构造函数
     * 
     * @param cardId 卡号
     * @param tradeType 交易类型
     * @param tradeMoney 交易金额
     * @param remark 备注信息
     */
    public TradeInfo(String cardId, String tradeType, BigDecimal tradeMoney, String remark) {
        this.cardId = cardId;
        this.tradeType = tradeType;
        this.tradeMoney = tradeMoney;
        this.remark = remark;
        this.tradeDate = LocalDateTime.now();
    }

    /**
     * 转账交易构造函数
     * 
     * @param cardId 卡号
     * @param tradeType 交易类型
     * @param tradeMoney 交易金额
     * @param targetCardId 目标卡号
     * @param remark 备注信息
     */
    public TradeInfo(String cardId, String tradeType, BigDecimal tradeMoney, String targetCardId, String remark) {
        this.cardId = cardId;
        this.tradeType = tradeType;
        this.tradeMoney = tradeMoney;
        this.targetCardId = targetCardId;
        this.remark = remark;
        this.tradeDate = LocalDateTime.now();
    }

    /**
     * 交易类型常量
     */
    public static class TradeType {
        public static final String DEPOSIT = "存入";
        public static final String WITHDRAW = "支取";
        public static final String TRANSFER = "转账";
    }

    /**
     * 重写toString方法
     */
    @Override
    public String toString() {
        return "TradeInfo{" +
                "tradeId=" + tradeId +
                ", cardId='" + cardId + '\'' +
                ", tradeDate=" + tradeDate +
                ", tradeType='" + tradeType + '\'' +
                ", tradeMoney=" + tradeMoney +
                ", remark='" + remark + '\'' +
                ", targetCardId='" + targetCardId + '\'' +
                '}';
    }
}
